package com.yyh.photo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.yyh.photo.common.R;
import com.yyh.photo.service.impl.QiniuServiceImpl;
import com.yyh.photo.vo.SysFileVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 文件请求处理
 */
@RestController
public class SysFileController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

    @Autowired
    QiniuServiceImpl qiniuService;

    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest req) {
        String realPath = "D://";
        // req.getSession().getServletContext().getRealPath("/uploadFile/");
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        String filePath = "";
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() +
                oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            uploadFile.transferTo(new File(folder, newName));
            filePath = req.getScheme() + "://" + req.getServerName() + ":" +
                    req.getServerPort() + "/uploadFile/" + format + newName;

        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败! ";
        }
        return filePath;
    }

    @ApiOperation(value = "文件上传")
    @RequestMapping("/uploadFile")
    public R uploadByQiniu(@RequestParam("file") MultipartFile uploadFile) {
        if (!uploadFile.isEmpty()) {
            String file = uploadFile.getOriginalFilename();
            JSONObject js =new JSONObject();
            js.put("fileName",file);
            js.put("fileUrl",qiniuService.saveFile(uploadFile));
            return R.ok().data(js);
        }
        return R.error().data("上传失败!");
    }
    @ApiOperation(value = "文件批量上传")
    @PostMapping("/uploads")
    public List<String> upload(@RequestParam("files") MultipartFile[] uploadFiles, HttpServletRequest req) {
        if (!ObjectUtils.isEmpty(uploadFiles) && uploadFiles.length > 0) {
            return qiniuService.saveFile(uploadFiles);
        }
        return Collections.singletonList("上传失败!");
    }
}